#include "CH58x_common.h"
#include "UTC_timestamp.h"

/**
 * @brief  判断闰年平年
 * @note
 * @param  None
 * @retval None
 * @author PWH
 * @date
 */
uint8_t Isleapyear(uint16_t year)
{
	if (((year) % 4 == 0 && (year) % 100 != 0) || (year) % 400 == 0)
		return 1; //是闰年
	return 0;   //是平年
}

/*
 *  函数功能:根据具体日期得到星期
 *  吉姆拉尔森公式  week=(date+2*month+3*(month+1)/5+year+year/4-y/100+y/400)%7
 *  注 : 把一月 二月看成是上一年的13 14月    ,    得到结果 0 -- 6
 * @ CSDN Tyrion.Mon
 */
void getWEEK(RTC_DateTimeTypeDef *time)
{
	uint16_t YY = 0;
	uint8_t MM = 0;
	if (time->month == 1 || time->month == 2)
			{
		MM = time->month + 12;
		YY = time->year - 1;
	}
	else
	{
		MM = time->month;
		YY = time->year;
	}
	time->week = ((time->date + 2 * MM + 3 * (MM + 1) / 5 + YY + YY / 4 - YY / 100 + YY / 400) % 7) + 1;
}

uint32_t UTC_mkTime(unsigned int year, unsigned int mon,
		unsigned int day, unsigned int hour,
		unsigned int min, unsigned int sec)
{
	if (0 >= (int) (mon -= 2)) { /**//* 1..12 -> 11,12,1..10 */
		mon += 12; /**//* Puts Feb last since it has leap day */
		year -= 1;
	}

	return (((
			(unsigned long) (year / 4 - year / 100 + year / 400 + 367 * mon / 12 + day) +
					year * 365 - 719499
			) * 24 + hour /**//* now have hours */
	) * 60 + min /**//* now have minutes */
	) * 60 + sec; /**//* finally seconds */
}

void UTC_getDate(RTC_DateTimeTypeDef *time, uint32_t Timestamp)
{

	uint16_t year = 1970;
	uint32_t Counter = 0, CounterTemp; //随着年份迭加，Counter记录从1970 年 1 月 1 日（00:00:00 GMT）到累加到的年份的最后一天的秒数
	uint8_t Month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	uint8_t i;

	while (Counter <= Timestamp)    //假设今天为2018年某一天，则时间戳应小于等于1970-1-1 0:0:0 到 2018-12-31 23:59:59的总秒数
	{
		CounterTemp = Counter;			 //CounterTemp记录完全1970-1-1 0:0:0 到 2017-12-31 23:59:59的总秒数后退出循环
		Counter += 31536000;//加上今年（平年）的秒数
		if (Isleapyear(year))
		{
			Counter += 86400; //闰年多加一天
		}
		year++;
	}
	time->year = year - 1; //跳出循环即表示到达计数值当前年
	Month[1] = (Isleapyear(time->year) ? 29 : 28);
	Counter = Timestamp - CounterTemp; //Counter = Timestamp - CounterTemp  记录2018年已走的总秒数
	CounterTemp = Counter / 86400;        //CounterTemp = Counter/(24*3600)  记录2018年已【过去】天数
	Counter -= CounterTemp * 86400;      //记录今天已走的总秒数
	time->hour = Counter / 3600; //时      				 得到今天的小时
	time->minute = Counter % 3600 / 60; //分
	time->second = Counter % 60; //秒
	for (i = 0; i < 12; i++)
			{
		if (CounterTemp < Month[i])    									//不能包含相等的情况，相等会导致最后一天切换到下一个月第一天时
				{
			//（即CounterTemp已走天数刚好为n个月完整天数相加时（31+28+31...）），
			time->month = i + 1;			  									// 月份不加1，日期溢出（如：出现32号）
			time->date = CounterTemp + 1; 								//应不作处理，CounterTemp = Month[i] = 31时，会继续循环，月份加一，
			break;																				//日期变为1，刚好符合实际日期
		}
		CounterTemp -= Month[i];
	}
	getWEEK(time);
}

